package com.leilei.controller;

import com.leilei.dao.impl.UserServiceImpl;
import com.leilei.entity.Address;
import com.leilei.entity.User;
import com.leilei.service.ITadministratorService;
import com.leilei.service.IUserService;
import com.leilei.service.impl.TadministratorServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet{

    private IUserService userService = new UserServiceImpl();
    private ITadministratorService tadministratorService = new TadministratorServiceImpl();

    public UserServlet(){

    }

    /**
     * 登录
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if ("admin".equals(username)){

            Tadministrator tadministrator = tadministratorService.selectAdminByUP(username, password);

            if (tadministrator.getId() != 0){
                req.getRequestDispatcher("managerIndex.jsp").forward(req, resp);
            } else{  //登录失败

                req.getRequestDispatcher("/login.jsp").forward(req, resp);
            }

        } else{

            User user = userService.selectUserByUP(username, password);
            //成功登录
            if (user.getUid() != 0){

                HttpSession session = req.getSession();
                session.setAttribute("user", user);
                //查询该账号购物车信息

                req.getRequestDispatcher("/index.jsp").forward(req, resp);

            } else{  //登录失败

                req.getRequestDispatcher("/login.jsp").forward(req, resp);
            }
        }

    }

    /**
     * 注册
     */
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String name = req.getParameter("name");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String addr = req.getParameter("address");
        String tel = req.getParameter("tel");

        if (userService.selectUidByUsername(username) > 0) {

            req.getRequestDispatcher("/register.jsp").forward(req, resp);
        }else{

            User user = new User();
            user.setName(name);
            user.setUsername(username);
            user.setPassword(password);
            user.setAddress(addr);
            user.setRegisterDate(new Date());

            Address address = new Address();
            address.setAddress(addr);
            address.setTel(tel);
            List<Address> addressList = new ArrayList<>();
            addressList.add(address);
            user.setAddressList(addressList);

            boolean result = userService.saveUser(user);

            //成功注册
            if (result){

                User u = userService.selectUserByUP(username, password);

                HttpSession session = req.getSession();
                session.setAttribute("user", u);
                req.getRequestDispatcher("/index.jsp").forward(req, resp);

            } else{  //注册失败

                req.getRequestDispatcher("/register.jsp").forward(req, resp);
            }
        }

    }

    /**
     * 根据username查询uid
     */
    protected void selectUidByUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");

        int result = userService.selectUidByUsername(username);

        //成功注册
        if (result > 0){

            resp.getWriter().write("账号已被注册");

        } else{  //注册失败

            resp.getWriter().write("账号可用");
        }

    }

    /**
     * 注销
     */
    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //保存购物车信息

        //用户设置为null

        //购物车设置为null

        //跳转回index页面


    }
}
